package plugin.eos.awake.flutter.awake1;

import android.text.TextUtils;

import com.oraclechain.pocketeos.bean.Account;
import com.oraclechain.pocketeos.bean.ForceAccountBean;
import com.oraclechain.pocketeos.blockchain.WalletAccountType;
import com.oraclechain.pocketeos.blockchain.cypto.ec.EosPrivateKey;

import io.flutter.Log;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import plugin.eos.awake.flutter.awake1.network.ApiService;
import plugin.eos.awake.flutter.awake1.network.MySubscriber;
import plugin.eos.awake.flutter.awake1.network.RetrofitManager;

/** Awake1Plugin */
public class Awake1Plugin implements MethodCallHandler {
  /** Plugin registration. */
  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "awake_plugin_eos");
    channel.setMethodCallHandler(new Awake1Plugin());
  }

  @Override
  public void onMethodCall(MethodCall call, Result result) {
    Log.d("xxx","onMethodCall method = " + call.method + " call.arguments = " + call.arguments);
    if ("getPlatformVersion".equals(call.method)) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if("getPublicKey".equals(call.method)){
//      result.success("publickey:" + getPublicKey((String) call.arguments) );
//      result.notImplemented();
        String privateKey = "5KHBqqZqSbm9PZyXqC481ymt7CVJ43v1mV9WP4YVP4U78Z6hr4S" ;
        Log.d("xxx", "(String) call.arguments = " + call.arguments);

        EosPrivateKey key;
        try {
            key = new EosPrivateKey(privateKey, WalletAccountType.EOS);
            getUserInfo(result,key);
        } catch (Exception e) {
            Log.d("xxx", "错误私钥 e " +  e );
        }
    }
  }

  private String getPublicKey(String privateKey){
    EosPrivateKey key;
    try {
      key = new EosPrivateKey(privateKey, WalletAccountType.EOS);
    } catch (Exception e) {
      return "错误私钥";
    }
    return key.getPublicKey().toString();
  }

  private void getUserInfo(Result result ,EosPrivateKey key){
    String publicKey = key.getPublicKey().toString();
    if (!TextUtils.isEmpty(publicKey)) {
      Observable<ForceAccountBean> request;
      request = RetrofitManager.get( ApiService.class)
              .getEOsKeyAccounts(new Account(publicKey));
        Log.d("xxx","request = " + request);
      request.subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new MySubscriber<ForceAccountBean>("history/get_key_accounts") {
                @Override
                protected void _onNext(ForceAccountBean forceAccountBean) {
                    Log.d("xxx","_onNext = " );
                    result.success(forceAccountBean.account_names.get(0));
                }

                @Override
                protected void _onError(Throwable e, String message) {
                    Log.d("xxx","_onError = message = "+ message );

                }
              });
    }
  }
}
